import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import('../views/Login.vue')
const Lyaout = () => import('../views/Lyaout.vue')
const Index = () => import('../views/index.vue')

const menu = () => import('../views/menu/index.vue')
const role = () => import('../views/role/index.vue')
const user = () => import('../views/user/index.vue')
//商品分类管理
const category = () => import('../views/category/index.vue')
const goods = () => import('../views/goods/index.vue')

// const specs = () => import('../views/specs/index.vue')


Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    // name: 'home',
    component: Login,
    meat: {
      title: '欢迎登录'
    }
  },
  {
    path: '/',
    component: Lyaout,
    children: [
      { path: '', component: Index },
      { path: 'menu', component: menu },
      { path: 'role', component: role },
      { path: 'user', component: user },
      { path: 'category', component: category },
      // { path: 'specs', component: specs }
      { path: 'goods', component: goods }


    ]

  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//路由守卫
// 路由拦截的目的是非法用户不能进入指定页面，看不到页面结构和具体内容
// axios拦截的目的是，即使非法用户进入了页面，后台也不返回数据
router.beforeEach((to, from, next) => {
  document.title = to.meta.title || "后台管理系统"
  //登录验证 
  let info = JSON.parse(localStorage.getItem('userInfo')) || {};
  if (info.token) {
    //token存在表示已经登录
    next()
  } else {
    //未登录可以访问首页和登录页
    if (to.path === '/' || to.path === '/login') {
      next()
    } else {
      //其他页面去登录页
      next('./login')
    }
  }
  // next()

})
export default router
